package com.taobao.wlb.mywlb;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;

/**  
 * Filename:    MockWebSQL.java  
 * Description:   
 * Copyright:   Copyright (c)2010  
 * Company:     taobao  
 * @author:     lanbo 
 * @version:    1.0  
 * Create at:   2011-9-21 下午02:58:31  
 *  
 * Modification History:  
 * Date         Author      Version     Description  
 * ------------------------------------------------------------------  
 * 2011-9-21      lanbo        1.0        Version  
 */
public class MockWebSQL {

	/**
	 * @param args
	 * @throws IOException 
	 * @throws ClientProtocolException 
	 */
	@SuppressWarnings("deprecation")
	public static void main(String[] args) throws ClientProtocolException, IOException {
		DefaultHttpClient httpclient = new DefaultHttpClient();
		HttpPost httpost = new HttpPost("https://backyard.seraph.taobao.com/login/?url=http://dba.tools.taobao.com:9999/workspace.htm");
		List<NameValuePair> nvps = new ArrayList<NameValuePair>();
		nvps.add(new BasicNameValuePair("id", "用戶名"));
		nvps.add(new BasicNameValuePair("pass_word", "密碼"));
		nvps.add(new BasicNameValuePair("url", "http%3A%2F%2Fdba.tools.taobao.com%3A9999%2Fworkspace.htm"));
		nvps.add(new BasicNameValuePair("action", "login"));
		httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
		HttpResponse response = httpclient.execute(httpost);
		HttpEntity entity = response.getEntity();
		System.out.println("Login form get: " + response.getStatusLine());
		if (entity != null) {
			entity.consumeContent();
		}
		//		System.out.println("Post logon cookies:");
		//		List<Cookie> cookies = httpclient.getCookieStore().getCookies();
		//		if (cookies.isEmpty()) {
		//			System.out.println("None");
		//		} else {
		//			for (int i = 0; i < cookies.size(); i++) {
		//				System.out.println("- " + cookies.get(i).toString());
		//			}
		//		}
		httpost = new HttpPost("http://dba.tools.taobao.com:9999/database/execute.jsn?_input_charset=UTF-8");
		nvps = new ArrayList<NameValuePair>();
		nvps.add(new BasicNameValuePair("dbId", "4682"));
		nvps.add(new BasicNameValuePair("sql", "select * from wlb_order where id = 1"));
		httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
		response = httpclient.execute(httpost);
		BufferedReader in = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "GBK"));
		StringBuffer sb = new StringBuffer("");
		String line = "";
		String NL = System.getProperty("line.separator");
		while ((line = in.readLine()) != null) {
			sb.append(line + NL);
		}
		in.close(); 
		System.out.println(sb.toString());
	}
}
